home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / usb.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-01-09  |  8.1 KB  |  337 lines

  1. /*
  2.  * Prototypes, structure definitions and macros.
  3.  *
  4.  * Copyright (c) 2000-2003 Johannes Erdfelt <johannes@erdfelt.com>
  5.  *
  6.  * This library is covered by the LGPL, read LICENSE for details.
  7.  *
  8.  * This file (and only this file) may alternatively be licensed under the
  9.  * BSD license as well, read LICENSE for details.
  10.  */
  11. #ifndef __USB_H__
  12. #define __USB_H__
  13.  
  14. #include <unistd.h>
  15. #include <stdlib.h>
  16. #include <limits.h>
  17.  
  18. #include <dirent.h>
  19.  
  20. /*
  21.  * USB spec information
  22.  *
  23.  * This is all stuff grabbed from various USB specs and is pretty much
  24.  * not subject to change
  25.  */
  26.  
  27. /*
  28.  * Device and/or Interface Class codes
  29.  */
  30. #define USB_CLASS_PER_INTERFACE        0    /* for DeviceClass */
  31. #define USB_CLASS_AUDIO            1
  32. #define USB_CLASS_COMM            2
  33. #define USB_CLASS_HID            3
  34. #define USB_CLASS_PRINTER        7
  35. #define USB_CLASS_MASS_STORAGE        8
  36. #define USB_CLASS_HUB            9
  37. #define USB_CLASS_DATA            10
  38. #define USB_CLASS_VENDOR_SPEC        0xff
  39.  
  40. /*
  41.  * Descriptor types
  42.  */
  43. #define USB_DT_DEVICE            0x01
  44. #define USB_DT_CONFIG            0x02
  45. #define USB_DT_STRING            0x03
  46. #define USB_DT_INTERFACE        0x04
  47. #define USB_DT_ENDPOINT            0x05
  48.  
  49. #define USB_DT_HID            0x21
  50. #define USB_DT_REPORT            0x22
  51. #define USB_DT_PHYSICAL            0x23
  52. #define USB_DT_HUB            0x29
  53.  
  54. /*
  55.  * Descriptor sizes per descriptor type
  56.  */
  57. #define USB_DT_DEVICE_SIZE        18
  58. #define USB_DT_CONFIG_SIZE        9
  59. #define USB_DT_INTERFACE_SIZE        9
  60. #define USB_DT_ENDPOINT_SIZE        7
  61. #define USB_DT_ENDPOINT_AUDIO_SIZE    9    /* Audio extension */
  62. #define USB_DT_HUB_NONVAR_SIZE        7
  63.  
  64. /* All standard descriptors have these 2 fields in common */
  65. struct usb_descriptor_header {
  66.     u_int8_t  bLength;
  67.     u_int8_t  bDescriptorType;
  68. };
  69.  
  70. /* String descriptor */
  71. struct usb_string_descriptor {
  72.     u_int8_t  bLength;
  73.     u_int8_t  bDescriptorType;
  74.     u_int16_t wData[1];
  75. };
  76.  
  77. /* HID descriptor */
  78. struct usb_hid_descriptor {
  79.     u_int8_t  bLength;
  80.     u_int8_t  bDescriptorType;
  81.     u_int16_t bcdHID;
  82.     u_int8_t  bCountryCode;
  83.     u_int8_t  bNumDescriptors;
  84.     /* u_int8_t  bReportDescriptorType; */
  85.     /* u_int16_t wDescriptorLength; */
  86.     /* ... */
  87. };
  88.  
  89. /* Endpoint descriptor */
  90. #define USB_MAXENDPOINTS    32
  91. struct usb_endpoint_descriptor {
  92.     u_int8_t  bLength;
  93.     u_int8_t  bDescriptorType;
  94.     u_int8_t  bEndpointAddress;
  95.     u_int8_t  bmAttributes;
  96.     u_int16_t wMaxPacketSize;
  97.     u_int8_t  bInterval;
  98.     u_int8_t  bRefresh;
  99.     u_int8_t  bSynchAddress;
  100.  
  101.     unsigned char *extra;    /* Extra descriptors */
  102.     int extralen;
  103. };
  104.  
  105. #define USB_ENDPOINT_ADDRESS_MASK    0x0f    /* in bEndpointAddress */
  106. #define USB_ENDPOINT_DIR_MASK        0x80
  107.  
  108. #define USB_ENDPOINT_TYPE_MASK        0x03    /* in bmAttributes */
  109. #define USB_ENDPOINT_TYPE_CONTROL    0
  110. #define USB_ENDPOINT_TYPE_ISOCHRONOUS    1
  111. #define USB_ENDPOINT_TYPE_BULK        2
  112. #define USB_ENDPOINT_TYPE_INTERRUPT    3
  113.  
  114. /* Interface descriptor */
  115. #define USB_MAXINTERFACES    32
  116. struct usb_interface_descriptor {
  117.     u_int8_t  bLength;
  118.     u_int8_t  bDescriptorType;
  119.     u_int8_t  bInterfaceNumber;
  120.     u_int8_t  bAlternateSetting;
  121.     u_int8_t  bNumEndpoints;
  122.     u_int8_t  bInterfaceClass;
  123.     u_int8_t  bInterfaceSubClass;
  124.     u_int8_t  bInterfaceProtocol;
  125.     u_int8_t  iInterface;
  126.  
  127.     struct usb_endpoint_descriptor *endpoint;
  128.  
  129.     unsigned char *extra;    /* Extra descriptors */
  130.     int extralen;
  131. };
  132.  
  133. #define USB_MAXALTSETTING    128    /* Hard limit */
  134. struct usb_interface {
  135.     struct usb_interface_descriptor *altsetting;
  136.  
  137.     int num_altsetting;
  138. };
  139.  
  140. /* Configuration descriptor information.. */
  141. #define USB_MAXCONFIG        8
  142. struct usb_config_descriptor {
  143.     u_int8_t  bLength;
  144.     u_int8_t  bDescriptorType;
  145.     u_int16_t wTotalLength;
  146.     u_int8_t  bNumInterfaces;
  147.     u_int8_t  bConfigurationValue;
  148.     u_int8_t  iConfiguration;
  149.     u_int8_t  bmAttributes;
  150.     u_int8_t  MaxPower;
  151.  
  152.     struct usb_interface *interface;
  153.  
  154.     unsigned char *extra;    /* Extra descriptors */
  155.     int extralen;
  156. };
  157.  
  158. /* Device descriptor */
  159. struct usb_device_descriptor {
  160.     u_int8_t  bLength;
  161.     u_int8_t  bDescriptorType;
  162.     u_int16_t bcdUSB;
  163.     u_int8_t  bDeviceClass;
  164.     u_int8_t  bDeviceSubClass;
  165.     u_int8_t  bDeviceProtocol;
  166.     u_int8_t  bMaxPacketSize0;
  167.     u_int16_t idVendor;
  168.     u_int16_t idProduct;
  169.     u_int16_t bcdDevice;
  170.     u_int8_t  iManufacturer;
  171.     u_int8_t  iProduct;
  172.     u_int8_t  iSerialNumber;
  173.     u_int8_t  bNumConfigurations;
  174. };
  175.  
  176. struct usb_ctrl_setup {
  177.     u_int8_t  bRequestType;
  178.     u_int8_t  bRequest;
  179.     u_int16_t wValue;
  180.     u_int16_t wIndex;
  181.     u_int16_t wLength;
  182. };
  183.  
  184. /*
  185.  * Standard requests
  186.  */
  187. #define USB_REQ_GET_STATUS        0x00
  188. #define USB_REQ_CLEAR_FEATURE        0x01
  189. /* 0x02 is reserved */
  190. #define USB_REQ_SET_FEATURE        0x03
  191. /* 0x04 is reserved */
  192. #define USB_REQ_SET_ADDRESS        0x05
  193. #define USB_REQ_GET_DESCRIPTOR        0x06
  194. #define USB_REQ_SET_DESCRIPTOR        0x07
  195. #define USB_REQ_GET_CONFIGURATION    0x08
  196. #define USB_REQ_SET_CONFIGURATION    0x09
  197. #define USB_REQ_GET_INTERFACE        0x0A
  198. #define USB_REQ_SET_INTERFACE        0x0B
  199. #define USB_REQ_SYNCH_FRAME        0x0C
  200.  
  201. #define USB_TYPE_STANDARD        (0x00 << 5)
  202. #define USB_TYPE_CLASS            (0x01 << 5)
  203. #define USB_TYPE_VENDOR            (0x02 << 5)
  204. #define USB_TYPE_RESERVED        (0x03 << 5)
  205.  
  206. #define USB_RECIP_DEVICE        0x00
  207. #define USB_RECIP_INTERFACE        0x01
  208. #define USB_RECIP_ENDPOINT        0x02
  209. #define USB_RECIP_OTHER            0x03
  210.  
  211. /*
  212.  * Various libusb API related stuff
  213.  */
  214.  
  215. #define USB_ENDPOINT_IN            0x80
  216. #define USB_ENDPOINT_OUT        0x00
  217.  
  218. /* Error codes */
  219. #define USB_ERROR_BEGIN            500000
  220.  
  221. /*
  222.  * This is supposed to look weird. This file is generated from autoconf
  223.  * and I didn't want to make this too complicated.
  224.  */
  225. #if 0
  226. #define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0)
  227. #else
  228. #define USB_LE16_TO_CPU(x)
  229. #endif
  230.  
  231. /* Data types */
  232. struct usb_device;
  233. struct usb_bus;
  234.  
  235. /*
  236.  * To maintain compatibility with applications already built with libusb,
  237.  * we must only add entries to the end of this structure. NEVER delete or
  238.  * move members and only change types if you really know what you're doing.
  239.  */
  240. struct usb_device {
  241.   struct usb_device *next, *prev;
  242.  
  243.   char filename[PATH_MAX + 1];
  244.  
  245.   struct usb_bus *bus;
  246.  
  247.   struct usb_device_descriptor descriptor;
  248.   struct usb_config_descriptor *config;
  249.  
  250.   void *dev;        /* Darwin support */
  251.  
  252.   u_int8_t devnum;
  253.  
  254.   unsigned char num_children;
  255.   struct usb_device **children;
  256. };
  257.  
  258. struct usb_bus {
  259.   struct usb_bus *next, *prev;
  260.  
  261.   char dirname[PATH_MAX + 1];
  262.  
  263.   struct usb_device *devices;
  264.   u_int32_t location;
  265.  
  266.   struct usb_device *root_dev;
  267. };
  268.  
  269. struct usb_dev_handle;
  270. typedef struct usb_dev_handle usb_dev_handle;
  271.  
  272. /* Variables */
  273. extern struct usb_bus *usb_busses;
  274.  
  275. #ifdef __cplusplus
  276. extern "C" {
  277. #endif
  278.  
  279. /* Function prototypes */
  280.  
  281. /* usb.c */
  282. usb_dev_handle *usb_open(struct usb_device *dev);
  283. int usb_close(usb_dev_handle *dev);
  284. int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf,
  285.     size_t buflen);
  286. int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf,
  287.     size_t buflen);
  288.  
  289. /* descriptors.c */
  290. int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep,
  291.     unsigned char type, unsigned char index, void *buf, int size);
  292. int usb_get_descriptor(usb_dev_handle *udev, unsigned char type,
  293.     unsigned char index, void *buf, int size);
  294.  
  295. /* <arch>.c */
  296. int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,
  297.     int timeout);
  298. int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
  299.     int timeout);
  300. int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,
  301.         int timeout);
  302. int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size,
  303.         int timeout);
  304. int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
  305.     int value, int index, char *bytes, int size, int timeout);
  306. int usb_set_configuration(usb_dev_handle *dev, int configuration);
  307. int usb_claim_interface(usb_dev_handle *dev, int interface);
  308. int usb_release_interface(usb_dev_handle *dev, int interface);
  309. int usb_set_altinterface(usb_dev_handle *dev, int alternate);
  310. int usb_resetep(usb_dev_handle *dev, unsigned int ep);
  311. int usb_clear_halt(usb_dev_handle *dev, unsigned int ep);
  312. int usb_reset(usb_dev_handle *dev);
  313.  
  314. #if 1
  315. #define LIBUSB_HAS_GET_DRIVER_NP 1
  316. int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name,
  317.     unsigned int namelen);
  318. #define LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP 1
  319. int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface);
  320. #endif
  321.  
  322. char *usb_strerror(void);
  323.  
  324. void usb_init(void);
  325. void usb_set_debug(int level);
  326. int usb_find_busses(void);
  327. int usb_find_devices(void);
  328. struct usb_device *usb_device(usb_dev_handle *dev);
  329. struct usb_bus *usb_get_busses(void);
  330.  
  331. #ifdef __cplusplus
  332. }
  333. #endif
  334.  
  335. #endif /* __USB_H__ */
  336.  
  337.